package com.puhui.lib.utils;

import android.app.Activity;
import android.os.Looper;
import android.text.TextUtils;
import android.view.Gravity;
import android.widget.Toast;

import com.puhui.lib.BuildConfig;

import java.lang.ref.WeakReference;

/***
 * Created by tangjian on 16-5-17.
 */
@SuppressWarnings("all")
public class MyToastUtil {

    private static MyToastUtil util = new MyToastUtil();

    private MyToastUtil() {
    }

    public static MyToastUtil getInstance() {
        if (util == null) {
            synchronized (MyToastUtil.class) {
                if (null == util) {
                    util = new MyToastUtil();
                }
            }
        }
        return util;
    }

    private Toast toast;
    //应当保存只有一个实例的Activity上下文，比如MainActivity
    private WeakReference<Activity> mContext;

    /**
     * 因为ToastUtils是单例的，所以Toast.makeText(context, text, Toast.LENGTH_SHORT);这条语句是会出现context内存泄漏的
     * <p>
     * 因此将mContext提取出来单独初始化，且只初始化一次
     * 该方法最好在MainActivity里面调用，因为MainActivity一般是不会finish掉的，就不会有内存泄漏产生了
     *
     * @param context
     */
    public MyToastUtil register(Activity context) {
//        if (null == mContext) {
        mContext = new WeakReference<>(context);
//        }
        return util;
    }

    /**
     * 在MainActivity中两次退出应用时调用
     */
    public void unRegister() {
        mContext = null;
    }

    /**
     * 显示Toast
     */
    public void show(CharSequence text) {
        show(text, false);
    }

    /**
     * 显示Toast
     */
    public void show(CharSequence text, boolean showAtCenter) {
        if (Thread.currentThread() != Looper.getMainLooper().getThread()) {
            if (BuildConfig.DEBUG) LogUtils.INSTANCE.printE("toast showed in non original thread");
            //CalledFromWrongThreadException
            //Only the original thread that created a view hierarchy can touch its views
            return;
        }

        if (!canShowToast()) {
            if (BuildConfig.DEBUG)
                LogUtils.INSTANCE.printE("can not show toast，you can check mContext's value." +
                        " Maybe mContext is finished, you should call method unregister() when you want to exit app.");
            return;
        }

        if (TextUtils.isEmpty(text)) {
            return;
        }

        if (toast == null) {
            toast = Toast.makeText(mContext.get(), text, Toast.LENGTH_SHORT);
        } else {
            toast.setText(text);
            toast.setDuration(Toast.LENGTH_SHORT);
        }
        if (showAtCenter) {
            toast.setGravity(Gravity.CENTER, 0, 0);
        }
        toast.show();
    }

    /**
     * 显示Toast
     */
    public void show(int resId) {
        show(resId, false);
    }

    /**
     * 显示Toast
     */
    public void show(int resId, boolean showAtCenter) {
        if (canShowToast()) {
            try {
                String toastStr = mContext.get().getString(resId);
                show(toastStr);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 判断是否可以弹出吐司
     */
    private boolean canShowToast() {
        return null != mContext && null != mContext.get() && !mContext.get().isFinishing();
    }
}
